static String decapitalize(String s) {
    List<Character> letters = s.split('_').inject('') {x, y -> "${x}${capitalize(y)}" }.toCharArray().toList()
    String firstLetter = letters.remove(0).toLowerCase()
    return "${firstLetter}${letters.inject('') {x, y -> "${x}${y}"}}"
}

static String makeStringable(String s) {
    StringBuffer sb = new StringBuffer("\"\"")
    for (String line: s.split('\n')) {
        sb.append("+\n\"").append(line).append("\"")
    }
    return sb.toString()


}

static String capitalize(String s){
    List<Character> letters = s.toCharArray().toList()
    Character firstLetter = letters.remove(0)
    return "${firstLetter.toUpperCase()}${letters.inject(''){x,y-> "${x}${y}"}}"
}


static String toClassName(String s) {
    capitalize(decapitalize(s))
}

Collection<String> rawStatements = []
StringBuffer sqlEnum = new StringBuffer("public static enum DataBaseQuery {")
StringBuffer classes = new StringBuffer()
StringBuffer tableNames = new StringBuffer()
File f = new File('createLite.sql')
String[] lines = f.getText().split('\n')
int[] starts = lines.findIndexValues { it ==~ /CREATE TABLE.*/}

for (int start: starts) {
    int end = (int) (start..lines.length).find { lines[it] ==~ /\s*\);\s*/}
    String statement = (start..end).inject('') {x, y -> "${x}${lines[y]}\n"}
    rawStatements << statement
}

boolean firstStatement = true
for (String statement: rawStatements) {

    classes

    if (!firstStatement) {
        classes.append('\n')
        sqlEnum.append(',\n')
    } else {
        firstStatement = false
    }

    String tableName = (statement =~ /\s*CREATE\s*TABLE\s*(.*)\s* \(/)[0][1]
    Collection<String> attributes = statement.split('\n').findAll { it.trim() ==~ /[a-z].*/}
    Map<String, String> attributeNamesDeclaration = [:]
    for (String attribute: attributes) {

        String attributeName = (attribute =~ /\s*(\S+)\s*.*/)[0][1]
        attributeNamesDeclaration.put(attributeName, attribute)

    }

    Map<String, String> attributesJavaToSql = new TreeMap<String, String>()
    attributesJavaToSql

    sqlEnum.append(tableName.toUpperCase()).append('(\n') // CREATE_MEDIA(
    sqlEnum.append(makeStringable(statement)).append(')')
    sqlEnum

    classes.append("protected static class ").append(toClassName(tableName)).append("{\n")

    attributeNamesDeclaration.keySet().each {
        classes.append("\tprotected static final String ").append(it.toUpperCase()).append(" = \"")
        classes.append(it).append("\";\n")
    }

    classes.append("}\n")

    tableNames.append("protected static final String ").append(tableName.toUpperCase())  \
   .append(" = \"").append(tableName).append("\";\n")
}
sqlEnum.append(";\n").append("private String query;\n").append("\n").append("DataBaseQuery(String query) {\n").append("this.query = query;\n").append("}\n").append("\n").append("public String getQuery() {\n")  \
.append("return query;\n").append("}\n").append("\n").append(" \n ").append(" } ")


//println tableNames
//println sqlEnum.toString()

/*
    CREATING THE NEW FILE
*/

String template = new File('DBNames.temp').getText()
File newFile = new File('DBNames.java')
newFile.append(template.replace(/@TABLE_CLASSES@/, classes.toString()).replace(/@ENUM@/, sqlEnum.toString()).replaceAll(/@TABLE_NAMES@/, tableNames.toString()))
